package com.qf.administration.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.administration.entity.ConferenceResource;
import com.qf.administration.mapper.ConferenceResourceMapper;
import com.qf.administration.qo.ResourceQO;
import com.qf.administration.service.ResourceService;
import com.qf.administration.utils.CommonBeanUtils;
import com.qf.administration.vo.ResourceVO;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @Title: ResourceServiceImpl
 * @Description:
 * @author:ZWX
 * @date: 2023/5/22 11:52
 */
@Service
public class ResourceServiceImpl implements ResourceService {
    @Resource
    private ConferenceResourceMapper conferenceResourceMapper;

    /*添加资源管理信息*/
    @Override
    public RespResult<String> addResource(ResourceQO resourceQO) {
        ConferenceResource conferenceResource = conferenceResourceMapper.selectById(resourceQO.getId());
        if (ObjectUtils.isEmpty(conferenceResource)) {
            ConferenceResource conferenceResource1 = new ConferenceResource();
            BeanUtils.copyProperties(resourceQO, conferenceResource1);
            int insert = conferenceResourceMapper.insert(conferenceResource1);
            if (insert > 0) {
                return RespResult.success("添加成功");
            } else {
                throw new ServiceException(ResultCode.INSERT_ERROR);
            }
        } else {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    /*删除资源管理信息*/
    @Override
    public RespResult<String> deleteResource(int[] ids) {
        int i = conferenceResourceMapper.deleteByResourceId(ids);
        if (i != ids.length) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        } else {
            return RespResult.success("删除成功");
        }
    }

    /*修改会议资源信息*/
    @Override
    public RespResult<String> updateResource(ResourceQO resourceQO) {
        ConferenceResource conferenceResource = new ConferenceResource();
        BeanUtils.copyProperties(resourceQO, conferenceResource);
        int i = conferenceResourceMapper.updateById(conferenceResource);
        if (i < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("更新成功");
    }

    @Override
    public PageInfo<ResourceVO> queryResource(int page, int size, String deviceName, int deviceStatus) {
        PageInfo<ConferenceResource> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> conferenceResourceMapper.selectList(deviceName, deviceStatus));
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return CommonBeanUtils.copyPageInfo(pageInfo, new PageInfo<>(), ResourceVO::new);
    }

    @Override
    public ResourceVO queryResourceOne(int id) {
        ConferenceResource conferenceResource = conferenceResourceMapper.selectById(id);
        if (ObjectUtils.isEmpty(conferenceResource)) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        } else {
            ResourceVO resourceVO = new ResourceVO();
            BeanUtils.copyProperties(conferenceResource, resourceVO);
            return resourceVO;
        }
    }
}
